using RxjhServer.DbClss;
using System;
using System.Collections.Generic;
using System.Data;
using System.Timers;

namespace RxjhServer
{
	public class EventClass : IDisposable
	{
		private System.Timers.Timer 时间1;

		private System.Timers.Timer 时间2;

		private System.Timers.Timer 时间3;

		private System.Timers.Timer 时间4;

		private System.Timers.Timer 时间5;

		private readonly DateTime kssj;

		private DateTime kssjgj;

		private int kssjint;

		private int 势力战sj;

		public void 势力战第一滴血获得物品(Players play)
		{
			try
			{
				string[] array = World.势力战奖励个人物品.Split(',');
				if (array.Length != 0 && int.Parse(array[0]) != 0)
				{
					int num = -1;
					try
					{
						num = int.Parse(array[1]);
					}
					catch
					{
					}
					ItmeClass itmeClass = new ItmeClass();
					itmeClass = ((num != -1) ? ItmeClass.GetItmeID(num) : ItmeClass.GetItme(array[1]));
					if (itmeClass != null)
					{
						int num2 = 1;
						if (array.Length >= 3)
						{
							num2 = int.Parse(array[2]);
						}
						if (num2 < 1)
						{
							num2 = 1;
						}
						for (int i = 1; i <= num2; i++)
						{
							int num3 = play.得到包裹空位(play);
							if (num3 == -1)
							{
								break;
							}
							play.增加物品带属性(itmeClass.FLD_PID, num3, num2, int.Parse(array[3]), int.Parse(array[4]), int.Parse(array[5]), int.Parse(array[6]), int.Parse(array[7]), int.Parse(array[8]));
						}
						play.系统提示("恭喜您获得 " + itmeClass.ItmeNAME + " 第一滴血奖励", 10, "势力战奖励");
						World.conn.发送("全服公告|10|势力战|势力战第一滴血获得者是[" + play.UserName + "]恭喜其获得物品[" + itmeClass.ItmeNAME + "]");
						logo.获奖人数Log("势力战第一滴血获得者是[" + play.UserName + "]");
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战第一滴血获得物品 出错：" + ex?.ToString());
			}
		}

		public EventClass(int 几转)
		{
			try
			{
				DBA.ExeSqlCommand("DELETE FROM EventTop");
				World.势力战第一滴血 = 0;
				World.EventTop.Clear();
				kssj = DateTime.Now.AddMinutes(5.0);
				World.势力战进程 = 1;
				World.势力战正分数 = 0;
				World.势力战邪分数 = 0;
				World.几转势力战 = 几转;
				时间1 = new System.Timers.Timer(60000.0);
				时间1.Elapsed += 时间结束事件1;
				时间1.Enabled = true;
				时间1.AutoReset = true;
				时间结束事件1(null, null);
				World.conn.发送("势力战进程|" + World.势力战进程.ToString());
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战 EventClass 出错：" + ex?.ToString());
			}
		}

		public EventClass()
		{
			try
			{
				World.EventTop.Clear();
				kssj = DateTime.Now.AddMinutes(World.势力战准备时间分钟);
				World.势力战进程 = 1;
				World.势力战正分数 = 0;
				World.势力战邪分数 = 0;
				时间1 = new System.Timers.Timer(3000.0);
				时间1.Elapsed += 时间结束事件1;
				时间1.Enabled = true;
				时间1.AutoReset = true;
				时间结束事件1(null, null);
				World.conn.发送("势力战进程|" + World.势力战进程.ToString());
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战 EventClass 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件1(object source, ElapsedEventArgs e)
		{
			try
			{
				假人势力战移动();
				int num = (int)kssj.Subtract(DateTime.Now).TotalSeconds;
				if (num <= 0)
				{
					World.势力战进程 = 2;
					World.势力战正分数 = 0;
					World.势力战邪分数 = 0;
					World.conn.发送("势力战进程|" + World.势力战进程.ToString());
					num = 0;
				}
				kssjint = num;
				if (World.allConnectedChars.Count > 0)
				{
					foreach (Players value in World.allConnectedChars.Values)
					{
						if (value.人物坐标_地图 == 801)
						{
							value.发送势力战快开始消息(kssjint);
						}
					}
				}
				if (World.几转势力战 == 3)
				{
					World.conn.发送("发送公告|" + 0.ToString() + "|离2/3转仙魔战开始还剩" + (kssjint / 60).ToString() + "分钟,请从韦大宝处进入银币报名！");
				}
				if (World.几转势力战 == 4)
				{
					World.conn.发送("发送公告|" + 0.ToString() + "|离4转仙魔战战开始还剩" + (kssjint / 60).ToString() + "分钟。请从韦大宝处进入银币报名！");
				}
				if (kssjint <= 0)
				{
					bool flag = true;
					foreach (Players value2 in World.allConnectedChars.Values)
					{
						if (value2.人物坐标_地图 == 801)
						{
							flag = false;
						}
					}
					if (flag)
					{
						Dispose();
					}
					else
					{
						时间1.Enabled = false;
						时间1.Close();
						时间1.Dispose();
						时间1 = null;
						World.势力战进程 = 3;
						World.conn.发送("势力战进程|" + World.势力战进程.ToString());
						kssjgj = DateTime.Now.AddMinutes(World.势力战时长);
						时间2 = new System.Timers.Timer(10000.0);
						时间2.Elapsed += 时间结束事件2;
						时间2.Enabled = true;
						时间2.AutoReset = true;
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战 时间结束事件1 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件2(object source, ElapsedEventArgs e)
		{
			try
			{
				int num = World.势力战时间 = (int)kssjgj.Subtract(DateTime.Now).TotalSeconds;
				foreach (Players value in World.allConnectedChars.Values)
				{
					if (value.人物坐标_地图 == 801)
					{
						value.发送势力战消息();
					}
				}
				if (num <= 0)
				{
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
					时间2 = null;
					World.势力战进程 = 4;
					World.conn.发送("势力战进程|" + World.势力战进程.ToString());
					时间3 = new System.Timers.Timer(10000.0);
					时间3.Elapsed += 时间结束事件3;
					时间3.Enabled = true;
					时间3.AutoReset = false;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战 时间结束事件2 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件3(object source, ElapsedEventArgs e)
		{
			try
			{
				World.conn.发送("势力战进程|" + World.势力战进程.ToString());
				时间3.Enabled = false;
				时间3.Close();
				时间3.Dispose();
				时间3 = null;
				时间4 = new System.Timers.Timer(10000.0);
				时间4.Elapsed += 时间结束事件4;
				时间4.Enabled = true;
				时间4.AutoReset = false;
				foreach (EventTopClass value in World.EventTop.Values)
				{
					RxjhClass.保存势力战数据(value);
				}
				if (World.势力战正分数 > World.势力战邪分数)
				{
					势力战sj = 1;
					World.conn.发送("发送公告|" + 0.ToString() + "|仙魔战结束[仙族]胜利。");
				}
				else if (World.势力战正分数 == World.势力战邪分数)
				{
					势力战sj = 3;
					World.conn.发送("发送公告|" + 0.ToString() + "|仙魔战结束,双方平局。");
				}
				else
				{
					势力战sj = 2;
					World.conn.发送("发送公告|" + 0.ToString() + "|仙魔战结束[魔族]胜利。");
				}
				foreach (Players value2 in World.allConnectedChars.Values)
				{
					if (value2.人物坐标_地图 == 801)
					{
						value2.发送势力战结束消息(势力战sj);
						if (value2.势力战临时正邪值 == 势力战sj)
						{
							if (value2.势力战杀人数 >= 1)
							{
								jlwp(value2, 胜败: true);
							}
						}
						else if (value2.势力战杀人数 >= 1)
						{
							jlwp(value2, 胜败: false);
						}
					}
				}
				try
				{
					随机奖励();
				}
				catch (Exception ex)
				{
					Form1.WriteLine(1, "势力战 随机奖励() 出错：" + ex?.ToString());
					Form1.WriteLine(1, "势力战 随机奖励()  出错：" + ex?.ToString());
				}
				try
				{
					奖励前五名();
				}
				catch (Exception ex2)
				{
					Form1.WriteLine(1, "势力战 奖励前五名() 出错：" + ex2?.ToString());
					Form1.WriteLine(1, "势力战 奖励前五名()  出错：" + ex2?.ToString());
				}
			}
			catch (Exception ex3)
			{
				Form1.WriteLine(1, "势力战 时间结束事件3 出错：" + ex3?.ToString());
			}
			finally
			{
				World.EventTop.Clear();
			}
		}

		public void jlwp(Players Play, bool 胜败)
		{
			try
			{
				if (Play != null)
				{
					int num = 18000000;
					if (Play.追加状态列表.ContainsKey(1008000095))
					{
						追加状态类 追加状态类 = Play.追加状态列表[1008000095];
						num += 追加状态类.FLD_sj;
						追加状态类.时间结束事件();
					}
					追加状态类 追加状态类2 = new 追加状态类(Play, num, 1008000095, 1);
					Play.追加状态列表.Add(追加状态类2.FLD_PID, 追加状态类2);
					Play.FLD_人物_追加_经验百分比 += 0.2;
					Play.状态效果(BitConverter.GetBytes(1008000095), 1, num);
					Play.Player_WuXun += World.势力战基础奖励武勋;
					Play.更新武功和状态();
					Play.查百宝阁元宝数();
					Play.检察积分数据(10, 1);
					Play.保存元宝数据();
					Play.系统提示("恭喜您获得奖励" + World.势力战基础奖励武勋.ToString() + "武勋,赠品积分10个。", 10, "系統提示");
				}
				Play.更新武功和状态();
			}
			catch
			{
			}
		}

		public void 随机奖励()
		{
			List<Players> list = new List<Players>();
			try
			{
				foreach (Players value2 in World.allConnectedChars.Values)
				{
					if (value2.人物坐标_地图 == 801 && value2.势力战临时正邪值 == 势力战sj)
					{
						list.Add(value2);
					}
				}
				if (list.Count > 0 && World.势力战随机奖励物品 != 0)
				{
					int 势力战随机奖励物品 = World.势力战随机奖励物品;
					int index = RNG.Next(0, list.Count - 1);
					byte[] bytes = BitConverter.GetBytes(RxjhClass.GetDBItmeId());
					int num = list[index].得到包裹空位(list[index]);
					if (num != -1 && World.Itme.TryGetValue(势力战随机奖励物品, out ItmeClass value))
					{
						list[index].增加物品(bytes, BitConverter.GetBytes(势力战随机奖励物品), num, BitConverter.GetBytes(1), new byte[20]);
						World.全局提示("系統提示", 10, "胜方玩家[" + list[index].UserName + "]在仙魔战中获得" + value.ItmeNAME + ",奖励人物由系统随机抽选。");
					}
				}
				list.Clear();
				list = null;
			}
			catch (Exception ex)
			{
				list.Clear();
				list = null;
				Form1.WriteLine(1, "势力战 时间结束事件33 出错：" + ex?.ToString());
			}
		}

		public void 奖励前五名()
		{
			try
			{
				DataTable dataTable = RxjhClass.得到势力战数据();
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						Players players = World.检查玩家name(dataTable.Rows[i]["人物名"].ToString());
						if (players != null)
						{
							if (i >= 0 && i < 5)
							{
								if (World.势力战前五名奖励武勋 > 0)
								{
									players.Player_WuXun += World.势力战前五名奖励武勋;
									players.查百宝阁元宝数();
									players.检察积分数据(50, 1);
									players.保存元宝数据();
									players.系统提示("恭喜您获得奖励" + World.势力战前五名奖励武勋.ToString() + "武勋,赠品元宝50个。", 10, "系統提示");
									players.更新武功和状态();
								}
							}
							else if (i >= 5 && i < 10)
							{
								if (World.势力战五到十名奖励武勋 > 0)
								{
									players.Player_WuXun += World.势力战五到十名奖励武勋;
									players.查百宝阁元宝数();
									players.检察积分数据(30, 1);
									players.保存元宝数据();
									players.系统提示("恭喜您获得奖励" + World.势力战五到十名奖励武勋.ToString() + "武勋,赠品元宝30个。", 10, "系統提示");
									players.更新武功和状态();
								}
							}
							else if (i >= 10 && i < 20 && World.势力战十到二十名奖励武勋 > 0)
							{
								players.Player_WuXun += World.势力战十到二十名奖励武勋;
								players.查百宝阁元宝数();
								players.检察积分数据(20, 1);
								players.保存元宝数据();
								players.系统提示("恭喜您获得奖励" + World.势力战十到二十名奖励武勋.ToString() + "武勋,赠品元宝20个。", 10, "系統提示");
								players?.更新武功和状态();
							}
							else if (i >= 20 && i <= 100)
							{
								players.Player_WuXun += World.势力战二十到五十名奖励武勋;
								players.查百宝阁元宝数();
								players.检察积分数据(10, 1);
								players.保存元宝数据();
								players.系统提示("恭喜您获得奖励" + World.势力战二十到五十名奖励武勋.ToString() + "武勋,赠品元宝10个，1.2经验加成5小时", 10, "系統提示");
								players?.更新武功和状态();
							}
						}
					}
					dataTable.Dispose();
				}
			}
			catch
			{
			}
		}

		public void 时间结束事件4(object source, ElapsedEventArgs e)
		{
			try
			{
				World.势力战进程 = 5;
				World.conn.发送("势力战进程|" + World.势力战进程.ToString());
				Dispose();
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战 时间结束事件4 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件5(object source, ElapsedEventArgs e)
		{
			try
			{
				if (DateTime.Now.Hour == World.三转势力战开启小时)
				{
					World.AddNpc(15220, 0f, 0f, 801);
				}
				else
				{
					World.AddNpc(15136, 0f, 0f, 801);
				}
				时间5.Enabled = false;
				时间5.Close();
				时间5.Dispose();
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "势力战 时间结束事件5 出错：" + ex?.ToString());
			}
		}

		public void 假人势力战移动()
		{
			int num = World.势力战正人数 + World.势力战邪人数;
			Random random = new Random();
			foreach (Players value in World.allConnectedChars.Values)
			{
				if (value.离线自动打怪 == 1 && value.Player_Job_leve >= 2 && value.假人是否参与势力战 == 0 && num <= 35 && World.假人是否参加势力战 == 1)
				{
					int num2 = random.Next(0, 10);
					if (num2 == 6)
					{
						value.假人反击PKindex = 0;
						if (World.势力战正人数 == World.势力战邪人数)
						{
							World.假人势力战参与人数++;
							World.势力战正人数++;
							value.势力战临时正邪值 = 1;
							value.势力战派别 = "仙族";
							value.切换PK模式(2);
							World.conn.发送("势力战人数|" + World.势力战正人数.ToString() + "|" + World.势力战邪人数.ToString() + "| 假人" + num.ToString());
							value.移动(RNG.Next(780, 890), RNG.Next(-220, 220), 15f, 801);
							value.假人是否参与势力战 = 1;
						}
						else if (World.势力战正人数 > World.势力战邪人数)
						{
							World.假人势力战参与人数++;
							World.势力战邪人数++;
							value.势力战临时正邪值 = 2;
							value.势力战派别 = "魔族";
							value.切换PK模式(2);
							World.conn.发送("势力战人数|" + World.势力战正人数.ToString() + "|" + World.势力战邪人数.ToString() + "| 假人" + num.ToString());
							value.移动(RNG.Next(-780, -890), RNG.Next(-220, 220), 15f, 801);
							value.假人是否参与势力战 = 1;
						}
						else if (World.势力战正人数 < World.势力战邪人数)
						{
							World.假人势力战参与人数++;
							World.势力战正人数++;
							value.势力战临时正邪值 = 1;
							value.势力战派别 = "仙族";
							value.切换PK模式(2);
							World.conn.发送("势力战人数|" + World.势力战正人数.ToString() + "|" + World.势力战邪人数.ToString() + "| 假人" + num.ToString());
							value.移动(RNG.Next(780, 890), RNG.Next(-220, 220), 15f, 801);
							value.假人是否参与势力战 = 1;
						}
					}
				}
			}
		}

		public void Dispose()
		{
			try
			{
				foreach (Players value in World.allConnectedChars.Values)
				{
					if (value.人物坐标_地图 == 801)
					{
						value.势力战临时正邪值 = value.Player_Zx;
						value.势力战杀人数 = 0;
						value.势力战死亡数 = 0;
						value.移动(-5f, -50f, 15f, 1201);
						value.更新装备效果();
						value.更新人物数据(value);
						value.更新广播人物数据();
					}
				}
				World.势力战进程 = 0;
				World.势力战时间 = 0;
				World.势力战正分数 = 0;
				World.势力战邪分数 = 0;
				World.几转势力战 = 0;
				World.假人势力战参与人数 = 0;
				if (World.势力战掉线玩家.Count > 0)
				{
					World.势力战掉线玩家.Clear();
				}
				if (时间1 != null)
				{
					时间1.Enabled = false;
					时间1.Close();
					时间1.Dispose();
					时间1 = null;
				}
				if (时间2 != null)
				{
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
					时间2 = null;
				}
				if (时间3 != null)
				{
					时间3.Enabled = false;
					时间3.Close();
					时间3.Dispose();
					时间3 = null;
				}
				if (时间4 != null)
				{
					时间4.Enabled = false;
					时间4.Close();
					时间4.Dispose();
					时间4 = null;
				}
				if (时间5 != null)
				{
					时间5.Enabled = false;
					时间5.Close();
					时间5.Dispose();
					时间5 = null;
				}
				if (World.eve != null)
				{
					World.eve = null;
					World.delNpc(801, 15137);
				}
			}
			catch
			{
			}
			finally
			{
				World.conn.发送("势力战进程|" + World.势力战进程.ToString());
				World.势力战进程 = 0;
				World.势力战时间 = 0;
				World.势力战正分数 = 0;
				World.势力战邪分数 = 0;
				if (World.势力战掉线玩家.Count > 0)
				{
					World.势力战掉线玩家.Clear();
				}
				if (时间1 != null)
				{
					时间1.Enabled = false;
					时间1.Close();
					时间1.Dispose();
					时间1 = null;
				}
				if (时间2 != null)
				{
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
					时间2 = null;
				}
				if (时间3 != null)
				{
					时间3.Enabled = false;
					时间3.Close();
					时间3.Dispose();
					时间3 = null;
				}
				if (时间4 != null)
				{
					时间4.Enabled = false;
					时间4.Close();
					时间4.Dispose();
					时间4 = null;
				}
				if (时间5 != null)
				{
					时间5.Enabled = false;
					时间5.Close();
					时间5.Dispose();
					时间5 = null;
				}
				if (World.eve != null)
				{
					World.eve = null;
					World.delNpc(801, 15137);
				}
			}
		}
	}
}
